package com.vortex.cloud.zhsw.xinyang.util;

import org.apache.poi.ss.usermodel.*;

/**
 * @Author: ji
 * @Date: 2021/3/4 4:59 下午
 */
public class ExcelStyleUtil {


    /**
     * 设置单元格格式
     * @param sheet
     * @param rowNum
     * @param cellNum
     * @param color 颜色
     */
    public static void setCellStyleColor(Workbook workbook, Sheet sheet, Integer rowNum, Integer cellNum, short color) {
        Row row =sheet.getRow(rowNum);
        Cell cell =row.getCell(cellNum);
        Font font = workbook.createFont();
        font.setColor(color);
        CellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.setFont(font);
        newCellStyle.setAlignment(HorizontalAlignment.CENTER);
        newCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(newCellStyle);
    }

    /**
     *
     * @param workbook
     * @param sheet
     * @param colLength 列长度
     */
    public static void setDefaultColumnStyle(Workbook workbook, Sheet sheet, int colLength) {
        if (colLength <= 0) {
            return;
        }
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));

        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        for (int i = 0 ; i < colLength ; i++) {
            sheet.setDefaultColumnStyle(i, cellStyle);
        }
    }
}
